<?xml version="1.0"?>
<implementation>
    <settings>
        <protocol>raw</protocol>
    </settings>

    <functions>
        local ipAddress = ""
		local ipPort = 8082

		local socket = require("socket")

		local function logError(outputError)
			luup.log("Error MeoBox: " .. outputError)
		end
		 
		function SendCommand(command)
			local key = tonumber(command)
			if key ~= nil then
				if (ipAddress ~= "") then
					luup.io.write("key=" .. key .. "\n") 
				else
					return false
				end
			else
					return ("Error: Attribute is not a number")
			end
		end

		function ChannelByNumber(number)
			local channel = tonumber(number)
			if channel ~= nil then
				if (channel >= 0) then
					local lenght = string.len(number)
					local position
					for i = 1, lenght, 1 do
						position = string.sub(number,i,i) + 48
						SendCommand(position)
					end
				else
					logError(command .. "- Number out of range.(0-999)")
				end
			else
					logError("Attribute is not a number. ")
			end
		end

		function Navigation(navButton)
			local action = string.upper(navButton)
			if action == "UP" then
				SendCommand("38")
				return true
			elseif action == "DOWN" then
				SendCommand("40")
				return true
			elseif action == "LEFT" then
				SendCommand("37")
				return true
			elseif action == "RIGHT" then
				SendCommand("39")
				return true
			elseif action == "OK" then
				SendCommand("13")
				return true
			elseif action == "BACK" then
				SendCommand("8")
				return true
			elseif action == "EXIT" then
				SendCommand("27")
				return true
			elseif action == "VOD" then
				SendCommand("114")
				return true
			elseif action == "RECORDINGS" then
				SendCommand("115")
				return true
			elseif action == "TVGUIDE" then
				SendCommand("122")
				return true
			elseif action == "MENU" then
				SendCommand("36")
				return true
			else
				return false
			end
		end

		function MediaControl(mediaButton)	
			if action == "PLAYPAUSE" then
				SendCommand("119")
				return true
			elseif action == "STOP" then
				SendCommand("123")
				return true
			elseif action == "FASTFORWARD" then
				SendCommand("121")
				return true
			elseif action == "REWIND" then
				SendCommand("118")
				return true
			elseif action == "NEXT" then
				SendCommand("122")
				return true
			elseif action == "PREVIOUS" then
				SendCommand("117")
				return true
			elseif action == "REC" then
				SendCommand("225")
				return true
			else
				return false
			end
		end

		function VolumeControl(volButton)
			logError("MEOBOX: Vol Changed Requested - " .. volButton)
			local action = string.upper(volButton)
			if action == "VOLUP" then
				SendCommand("175")
				return true
			elseif action == "MUTE" then
				SendCommand("173")
				return true
			elseif action == "VOLDOWN" then
				SendCommand("174")
				return true
			else
				return false
			end
		end

		function ProgramControl(progButton)
			local action = string.upper(progButton)
			if action == "CHUP" then
				SendCommand("33")
				return true
			elseif action == "CHDOWN" then
				SendCommand("34")
				return true
			elseif action == "CHPREV" then
				SendCommand("156")
				return true
			elseif action == "INFO" then
				SendCommand("159")
				return true
			else
				return false
			end
		end

		function ColourKeys(COLOUR)
			local action = string.upper(COLOUR)
			if action == "RED" then
				SendCommand("140")
				return true
			elseif action == "GREEN" then
				SendCommand("141")
				return true
			elseif action == "YELLOW" then
				SendCommand("142")
				return true
			elseif action == "BLUE" then
				SendCommand("143")
				return true
			else
				return false
			end
		end

		function PowerToggle()
			SendCommand("233")
		end

		function MeoBoxStartup(lul_device)
            ipAddress = luup.devices[lul_device].ip
			luup.io.open(lul_device, ipAddress, ipPort)
        end
    </functions>

    <incoming><lua></lua></incoming>

    <startup>MeoBoxStartup</startup>

    <actionList>
        <action>
            <serviceId>urn:micasaverde-com:serviceId:TogglePower1</serviceId>
            <name>OnOff</name>
            <run>
                PowerToggle()
            </run>
        </action>
		
		<action>
            <serviceId>urn:micasaverde-com:serviceId:MenuNavigation1</serviceId>
            <name>Menu</name>
            <run>
                Navigation("MENU")
            </run>
        </action>
		
		<action>
            <serviceId>urn:micasaverde-com:serviceId:MenuNavigation1</serviceId>
            <name>Exit</name>
            <run>
               Navigation("EXIT")
            </run>
        </action>
		
        <action>
            <serviceId>urn:micasaverde-com:serviceId:MenuNavigation1</serviceId>
            <name>Back</name>
            <run>
                Navigation("BACK")
            </run>
        </action>

		<action>
            <serviceId>urn:micasaverde-com:serviceId:MenuNavigation1</serviceId>
            <name>Up</name>
            <run>
                Navigation("UP")
            </run> 
        </action>
		
        <action>
            <serviceId>urn:micasaverde-com:serviceId:MenuNavigation1</serviceId>
            <name>Down</name>
            <run>                
                Navigation("DOWN")
            </run>
        </action>

        <action>
            <serviceId>urn:micasaverde-com:serviceId:MenuNavigation1</serviceId>
            <name>Left</name>
            <run>
                Navigation("LEFT")
            </run>
        </action>

        <action>
            <serviceId>urn:micasaverde-com:serviceId:MenuNavigation1</serviceId>
            <name>Right</name>
            <run>
                Navigation("RIGHT")
            </run>
        </action>

        <action>
            <serviceId>urn:micasaverde-com:serviceId:MenuNavigation1</serviceId>
            <name>Select</name>
            <run>
                Navigation("OK")
            </run>
        </action>
		
		<action>
            <serviceId>urn:micasaverde-com:serviceId:MenuNavigation1</serviceId>
            <name>PageDown</name>
            <run>
                MediaControl("NEXT")
            </run>
        </action>
		
		<action>
            <serviceId>urn:micasaverde-com:serviceId:MenuNavigation1</serviceId>
            <name>PageUp</name>
            <run>
                Navigation("PREVIOUS")
            </run>
        </action>

        <action>
            <serviceId>urn:micasaverde-com:serviceId:MediaNavigation1</serviceId>
            <name>FastForward</name>
            <run>
                MediaControl("FASTFORWARD")
            </run>
        </action>

		<action>
            <serviceId>urn:micasaverde-com:serviceId:MediaNavigation1</serviceId>
            <name>Rewind</name>
            <run>
                MediaControl("REWIND")
            </run>
        </action>
		
        <action>
            <serviceId>urn:micasaverde-com:serviceId:MediaNavigation1</serviceId>
            <name>Pause</name>
            <run>
                MediaControl("PLAYPAUSE")
            </run>
        </action>

        <action>
            <serviceId>urn:micasaverde-com:serviceId:MediaNavigation1</serviceId>
            <name>Play</name>
            <run>
                MediaControl("PLAYPAUSE")
            </run>
        </action>

        <action>
            <serviceId>urn:micasaverde-com:serviceId:MediaNavigation1</serviceId>
            <name>SkipUp</name>
            <run>
                MediaControl("NEXT")
            </run>
        </action>
		
		<action>
            <serviceId>urn:micasaverde-com:serviceId:MediaNavigation1</serviceId>
            <name>SkipDown</name>
            <run>
                MediaControl("PREVIOUS")
            </run>
        </action>

        <action>
            <serviceId>urn:micasaverde-com:serviceId:MediaNavigation1</serviceId>
            <name>Stop</name>
            <run>
                MediaControl("STOP")
            </run>
        </action>

        <action>
            <serviceId>urn:micasaverde-com:serviceId:NumericEntry1</serviceId>
            <name>0</name>
            <run>
                ChannelByNumber("0")
            </run>
        </action>

        <action>
            <serviceId>urn:micasaverde-com:serviceId:NumericEntry1</serviceId>
            <name>1</name>
            <run>
                ChannelByNumber("1")
            </run>
        </action>

        <action>
            <serviceId>urn:micasaverde-com:serviceId:NumericEntry1</serviceId>
            <name>2</name>
            <run>
                ChannelByNumber("2")
            </run>
        </action>

        <action>
            <serviceId>urn:micasaverde-com:serviceId:NumericEntry1</serviceId>
            <name>3</name>
            <run>
                ChannelByNumber("3")
            </run>
        </action>

        <action>
            <serviceId>urn:micasaverde-com:serviceId:NumericEntry1</serviceId>
            <name>4</name>
            <run>
                ChannelByNumber("4")
            </run>
        </action>

        <action>
            <serviceId>urn:micasaverde-com:serviceId:NumericEntry1</serviceId>
            <name>5</name>
            <run>
                ChannelByNumber("5")
            </run>
        </action>

        <action>
            <serviceId>urn:micasaverde-com:serviceId:NumericEntry1</serviceId>
            <name>6</name>
            <run>
                ChannelByNumber("6")
            </run>
        </action>

        <action>
            <serviceId>urn:micasaverde-com:serviceId:NumericEntry1</serviceId>
            <name>7</name>
            <run>
                ChannelByNumber("7")
            </run>
        </action>

        <action>
            <serviceId>urn:micasaverde-com:serviceId:NumericEntry1</serviceId>
            <name>8</name>
            <run>
                ChannelByNumber("8")
            </run>
        </action>

        <action>
            <serviceId>urn:micasaverde-com:serviceId:NumericEntry1</serviceId>
            <name>9</name>
            <run>
                ChannelByNumber("9")
            </run>
        </action>

        <action>
            <serviceId>urn:micasaverde-com:serviceId:Tuning1</serviceId>
            <name>ChannelDown</name>
            <run>
                ProgramControl("CHDOWN")
            </run>
        </action>

        <action>
            <serviceId>urn:micasaverde-com:serviceId:Tuning1</serviceId>
            <name>ChannelEnter</name>
            <run>
                Navigation("OK")
            </run>
        </action>

        <action>
            <serviceId>urn:micasaverde-com:serviceId:Tuning1</serviceId>
            <name>ChannelUp</name>
            <run>
                ProgramControl("CHDOWN")
            </run>
        </action>

        <action>
            <serviceId>urn:micasaverde-com:serviceId:TV1</serviceId>
            <name>Guide</name>
            <run>
                Navigation("TVGUIDE")
            </run>
        </action>

        <action>
            <serviceId>urn:micasaverde-com:serviceId:Misc1</serviceId>
            <name>Info</name>
            <run>
                ProgramControl("INFO")
            </run>
        </action>

        <action>
            <serviceId>urn:micasaverde-com:serviceId:Misc1</serviceId>
            <name>Record</name>
            <run>
                MediaControl("REC")
            </run>
        </action>

        <action>
            <serviceId>urn:micasaverde-com:serviceId:Misc1</serviceId>
            <name>FastextRed</name>
            <run>
                ColourKeys("RED")
            </run>
        </action>

        <action>
            <serviceId>urn:micasaverde-com:serviceId:Misc1</serviceId>
            <name>FastextYellow</name>
            <run>
                ColourKeys("YELLOW")
            </run>
        </action>

        <action>
            <serviceId>urn:micasaverde-com:serviceId:Misc1</serviceId>
            <name>FastextGreen</name>
            <run>
                ColourKeys("GREEN")
            </run>
        </action>

        <action>
            <serviceId>urn:micasaverde-com:serviceId:Misc1</serviceId>
            <name>FastextBlue</name>
            <run>
                ColourKeys("BLUE")
            </run>
        </action>

        <action>
            <serviceId>urn:micasaverde-com:serviceId:Misc1</serviceId>
            <name>PPV</name>
            <run>
                Navigation("VOD")
            </run>
        </action>
		
		<action>
            <serviceId>urn:micasaverde-com:serviceId:Volume1</serviceId>
            <name>Down</name>
            <run>
                VolumeControl("VOLDOWN")
            </run>
        </action>
		
		<action>
            <serviceId>urn:micasaverde-com:serviceId:Volume1</serviceId>
            <name>Mute</name>
            <run>
                VolumeControl("MUTE")
            </run>
        </action>
		
		<action>
            <serviceId>urn:micasaverde-com:serviceId:Volume1</serviceId>
            <name>Up</name>
            <run>
                VolumeControl("VOLUP")
            </run>
        </action>
    </actionList>
</implementation>
